package mobidever.godutch.activity;

import mobidever.godutch.activity.base.ActivityFrame;
import mobidever.godutch.adapter.AdapterUser;
import mobidever.godutch.business.BusinessUser;
import mobidever.godutch.controls.SlideMenuView.OnSlideMenuListener;
import mobidever.godutch.controls.SliderMenuItem;
import mobidever.godutch.model.ModelUser;
import mobidever.godutch.utility.RegexTools;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class ActivityUser extends ActivityFrame implements OnSlideMenuListener {
	private ListView lvUserList;
	private AdapterUser mAdapterUser;
	private BusinessUser mBusinessUser;
	private ModelUser mSelectModelUser;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		AppendMainBody(R.layout.layout_user);
		InitVariable();
		InitView();
		InitListeners();
		BindData();
		CreateSlideMenu(R.array.SlideMenuUser);
	}

	public void InitVariable() {

		mBusinessUser = new BusinessUser(this);
	}

	public void InitView() {
		lvUserList = (ListView) findViewById(R.id.lvUserList);
	}

	public void InitListeners() {
		registerForContextMenu(lvUserList);
	}

	public void BindData() {
		mAdapterUser = new AdapterUser(this);
		lvUserList.setAdapter(mAdapterUser);
		SetTitle();
	}
	private void SetTitle(){
		int _Count = mAdapterUser.getCount();
		String _Titel = getString(R.string.ActivityTitleUser, new Object[]{_Count});
		SetTopBarTitle(_Titel);
		
	}
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		AdapterContextMenuInfo _AdapterContextMenuInfo = (AdapterContextMenuInfo) menuInfo;
		ListAdapter _ListAdapter = lvUserList.getAdapter();
		mSelectModelUser = (ModelUser) _ListAdapter
				.getItem(_AdapterContextMenuInfo.position);
		menu.setHeaderIcon(R.drawable.user_small_icon);
		menu.setHeaderTitle(mSelectModelUser.getmUserName());
		CreateMenu(menu);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case 1:
			ShowUserAddOrEditDialog(mSelectModelUser);
			break;
		case 2:
			Delete();
			break;
		default:
			break;
		}
		return super.onContextItemSelected(item);
	}

	@Override
	public void onSlideMenuItemClick(View pView, SliderMenuItem pSliderMenuItem) {
		SlideMenuToggle();
		if (pSliderMenuItem.getmItemID() == 0) {
			ShowUserAddOrEditDialog(null);
		}
	}

	private void ShowUserAddOrEditDialog(ModelUser pModelUser) {
		View _View = GetLayoutInflater().inflate(R.layout.user_add_or_edit,
				null);
		EditText _etUserName = (EditText) _View.findViewById(R.id.etUserName);
		if (pModelUser != null) {
			_etUserName.setText(pModelUser.getmUserName());
		}
		String _Title;
		if (pModelUser == null) {
			_Title = getString(R.string.DialogTitleUser,
					new Object[] { getString(R.string.TitleAdd) });
		} else {
			_Title = getString(R.string.DialogTitleUser,
					new Object[] { getString(R.string.TitleEdit) });
		}
		AlertDialog.Builder _Builder = new AlertDialog.Builder(this);
		_Builder.setTitle(_Title)//
				.setView(_View)//
				.setIcon(R.drawable.user_big_icon)
				//
				.setPositiveButton(
						getString(R.string.ButtonTextSave),
						new OnAddOrEditUserListener(pModelUser, _etUserName,
								true))//
				.setNegativeButton(getString(R.string.ButtonTextCancel),
						new OnAddOrEditUserListener(null, null, false))//
				.show();
	}

	private class OnAddOrEditUserListener implements
			DialogInterface.OnClickListener {
		private ModelUser mModelUser;
		private EditText etUserName;
		private boolean mIsSaveButton;

		public OnAddOrEditUserListener(ModelUser pModelUser,
				EditText etUserName, boolean mIsSaveButton) {
			this.mModelUser = pModelUser;
			this.etUserName = etUserName;
			this.mIsSaveButton = mIsSaveButton;
		}

		@Override
		public void onClick(DialogInterface dialog, int which) {
			if (mIsSaveButton == false) {
				setAlertDialogIsClose(dialog, true);
				return ;
			}
			if (mModelUser == null) {
				mModelUser = new ModelUser();
			}
			String _UserName = etUserName.getText().toString().trim();
			boolean _CheckResult = RegexTools.IsChineseEnglishNum(_UserName);
			if (_CheckResult) {
				Toast.makeText(
						getApplicationContext(),
						getString(R.string.CheckDataTextChineseEnglishNum,
								new Object[] { etUserName.getHint() }),
						SHOW_TIME).show();
				setAlertDialogIsClose(dialog, false);
			} else {
				setAlertDialogIsClose(dialog, true);
			}
			_CheckResult = mBusinessUser.IsExistUserByUserName(_UserName,
					mModelUser.getmUserID());
			if (_CheckResult) {
				Toast.makeText(getApplicationContext(),
						getString(R.string.CheckDataTextUserExist), SHOW_TIME)
						.show();
				setAlertDialogIsClose(dialog, false);
			} else {
				setAlertDialogIsClose(dialog, true);
			}
			mModelUser.setmUserName(etUserName.getText().toString());
			boolean _Result = false;
			if (mModelUser.getmUserID() == 0) {
				_Result = mBusinessUser.InsertUser(mModelUser);
			} else {
				_Result = mBusinessUser.UpdateUserByUserID(mModelUser);
			}
			if (_Result) {
				BindData();
			} else {
				Toast.makeText(getApplicationContext(),
						getString(R.string.TipsAddFail), SHOW_TIME).show();
			}
		}

	}

	private void Delete() {
		String _Message = getString(R.string.DialogMessageUserDelete,
				new Object[] { mSelectModelUser.getmUserName() });
		Log.i("mylog",getResources().getString(R.string.DialogTitleDelete));
		ShowAlertDialog(R.string.DialogTitleDelete, _Message,
				new OnDeleteClickListener());
	}

	private class OnDeleteClickListener implements
			DialogInterface.OnClickListener {

		@Override
		public void onClick(DialogInterface dialog, int which) {
			boolean _Result = mBusinessUser.HideUserByUserID(mSelectModelUser
					.getmUserID());
			if (_Result == true) {
				BindData();
			}
		}
	}
}